<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <back-top @click.native="scrollTo" v-show="isShowBackTop"></back-top>
    <scroll
      class="wrapper"
      ref="scroll"
      :probeType="3"
      @scroll="contentScroll"
      :pullUpLoad="true"
      @pullingUp="pullingUp"
    >
      <home-swiper :swiperdata="banners"> </home-swiper>
      <recommend :recommends="recommends"></recommend>
      <feature></feature>
      <tab-control
        :titles="['流行', '新歌', '精选']"
        class="tab-control"
        @tabClick="changetab"
      ></tab-control>
      <goods-list :goodsList="showGoods" class="goods-list"></goods-list>
    </scroll>
  </div>
</template>
<style scoped>
#home {
  padding-top: 44px;
}
.home-nav {
  background-color: deeppink;
  color: #fff;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 9;
}
.tab-control {
  position: sticky;
  top: 44px;
  z-index: 9999;
  background-color: #fff;
}
.wrapper {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}
</style>
<script>
import NavBar from "components/common/navbar/NavBar";
import { getHomeMultidata, getHomeGood } from "network/home";
import HomeSwiper from "./childrenHome/HomeSwiper.vue";
import Recommend from "./childrenHome/Recommend.vue";
import Feature from "./childrenHome/Feature.vue";
import TabControl from "components/content/tabcontrol/TabControl.vue";
import GoodsList from "components/content/goods/GoodsList.vue";
import Scroll from "components/common/scroll/Scroll.vue";
import BackTop from "components/content/backTop/BackTop.vue";
export default {
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      cuurentType: "pop",
      isShowBackTop: false,
    };
  },
  computed: {
    showGoods() {
      return this.goods[this.cuurentType].list;
    },
  },
  created() {
    this.getHomeMultidata();
    this.getHomeGood("new");
    this.getHomeGood("pop");
    this.getHomeGood("sell");
  },
  methods: {
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    getHomeGood(type) {
      const page = this.goods[type].page + 1;
      getHomeGood(type, page).then((res) => {
        this.goods[type].list.push(...res.data.list);

        this.goods[type].page += 1;
      });
    },
    //切换商品分类
    changetab(index) {
      switch (index) {
        case 0:
          this.cuurentType = "pop";
          break;
        case 1:
          this.cuurentType = "new";
          break;
        case 2:
          this.cuurentType = "sell";
          break;
      }
    },
    //点击返回顶部
    scrollTo() {
      this.$refs.scroll.scrollTo(0, 0, 500);
    },

    //页面滚动
    contentScroll(position) {
      this.isShowBackTop = position.y < -1000;
    },

    //上拉触底触发,加载更多商品
    pullingUp() {
      this.getHomeGood(this.cuurentType);
      console.log("触底");
    },
  },
  components: {
    NavBar,
    HomeSwiper,
    Recommend,
    Feature,
    TabControl,
    getHomeGood,
    GoodsList,
    Scroll,
    BackTop,
  },
};
</script>